﻿package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.ConsumerLogListAdapter;
import com.kuanter.kuanterauto.cache.ObjectCache;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.ConsumerLog;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.tencent.mm.sdk.platformtools.Log;
import com.umeng.analytics.MobclickAgent;

public class MyKuanterConsumerActivity extends WuyangBaseActivity {

	private List<ConsumerLog> consumerLogList;
	private ListView lv_consumer_log;
	private View back_btn;
	private Intent intent;
	private DataLoader dataLoader;
	private int userId;
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
				// String result = msg.getData().getString("result");
				String result = (String) msg.obj;
				Log.i("wuyang", result);
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
															// 字段值是否为false
							com.alibaba.fastjson.JSONObject jsonObjectResult = JSON
									.parseObject(result);
							String JsonStringUserInfo = jsonObjectResult
									.getString("consumptionList");
							if ("[]".equals(JsonStringUserInfo)) {
								finishProgress();
								showToast("记录暂无");
							} else {
								consumerLogList = JSON.parseArray(
										JsonStringUserInfo, ConsumerLog.class);
								// 将consumberList缓存
								ObjectCache
										.set("consumerList", consumerLogList);

								lv_consumer_log = (ListView) MyKuanterConsumerActivity.this
										.findViewById(R.id.lv_consumer_log);
								lv_consumer_log
										.setAdapter(new ConsumerLogListAdapter(
												consumerLogList,
												MyKuanterConsumerActivity.this));
								lv_consumer_log
										.setOnItemClickListener(new ConsumerLogListClickListener());
							}
							finishProgress();

						} else {
							finishProgress();
							// 显示返回数据中 errorMsg 字段值
							showToast(StringUtil.geterrorMsg(result));
						}
					} catch (JSONException e) {
						finishProgress();
						showToast("登录失败");
						e.printStackTrace();
					}
				} else {
					finishProgress();
					showToast("登录失败,网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
	@Override
	protected void onResume() {
		dataLoader = new DataLoader(this);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userConsumption.userId", userId+""));
		params.add(new BasicNameValuePair("startIndex",0+""));
		params.add(new BasicNameValuePair("Limit", null));
		dataLoader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.CONSUMER_LOG,
				params, handler, "", false);
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.my_consumer_log);
		super.onCreate(savedInstanceState);
		userId = SharePrefenceUtil.getUserID(this);

		// 联网去消费记录
		// 参数：用户id(int) userConsumption.userId
		// 回复数据：宽途帮您节省的钱数，消费记录列表（消费项目 ，金额，消费时间，交易状态，评分）
		dataLoader = new DataLoader(this);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userConsumption.userId", userId+""));
		params.add(new BasicNameValuePair("startIndex",0+""));
		params.add(new BasicNameValuePair("Limit", null));
		dataLoader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.CONSUMER_LOG,
				params, handler, "", false);
		showProgress("正在加载数据...");
		this.setHeaderControlViewText();
		this.initBodyControlView();
		this.setBodyControlViewText();
		this.registerListener();

	}

	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(super.headerTitle, "我的消费");
		super.setHeaderLeftText(super.headerLeft, "返回");
		super.setHeaderRightText(super.headerRight, null);
	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		back_btn = this.findViewById(R.id.header_left);
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		back_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
	}

	private class ConsumerLogListClickListener implements
			AdapterView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			intent = new Intent();
			intent.setClass(MyKuanterConsumerActivity.this,
					MyConsumerDetailActivity.class);
			intent.putExtra("consumerLogIndex", position);
			startActivity(intent);
		}

	}
}
